Change content folder location in prefs window

James Peret 8 years ago
parent
commit
b038a8a877

+ 1 - 0
app/index.html

@@ -57,6 +57,7 @@
57 57
             },
58 58
             {
59 59
               label: 'Preferences',
60
+              accelerator: 'CommandOrControl+,',
60 61
               click: function () {
61 62
                 ipc.send('show-preferences-window')
62 63
               }

+ 19 - 18
app/preferences-window.html

@@ -2,46 +2,47 @@
2 2
 <html  class="no-js">
3 3
 
4 4
   <head>
5
-    <title>Codex</title>
5
+    <title>Codex Preferences</title>
6 6
     <meta charset="UTF-8">
7 7
     <!-- Stylesheets -->
8 8
     <link rel="stylesheet" href="../css/tomorrow-night-eighties.css">
9 9
     <link rel="stylesheet" href="../css/photon.min.css">
10 10
     <link rel="stylesheet" href="../css/codex.css">
11 11
 
12
+    <script src="../bower_components/angular/angular.js"></script>
13
+    <script src="../bower_components/angular-ui-router/release/angular-ui-router.js"></script>
14
+    <script src="../bower_components/angular-sanitize/angular-sanitize.js"></script>
15
+
16
+    <script src="scripts/preferences.js"></script>
17
+    <script src="scripts/controllers/general-prefs-ctrl.js"></script>
18
+    <script src="scripts/controllers/databases-prefs-ctrl.js"></script>
19
+    <script src="scripts/controllers/prefs-nav-ctrl.js"></script>
20
+    <script src="scripts/services/file-service.js"></script>
21
+    <script src="scripts/services/thumbnail-service.js"></script>
22
+    <script src="scripts/services/prefs-service.js"></script>
23
+
12 24
     <script>
13 25
       var remote = require('remote');
14 26
       var ipc = require('ipc');
15 27
       var dialog = remote.require('dialog');
28
+      console.log("-> Opening Preferences Window")
16 29
     </script>
17 30
   </head>
18 31
 
19
-  <body>
32
+  <body ng-app="codexApp">
20 33
     <header class="toolbar toolbar-header">
21 34
       <h1 class="title">Preferences</h1>
22 35
 
23
-      <ul class="icon-tabs">
24
-        <li class="active">
36
+      <ul class="icon-tabs" ng-controller="PrefsNavCtrl">
37
+        <li ng-class="links[0]" ng-click="goToGeneral()">
25 38
           <img src="content/imgs/prefs-icon.png" style="width: 32px; height: 32px;"><br>General
26 39
         </li>
27
-        <li>
40
+        <li ng-class="links[1]" ng-click="goToDatabases()">
28 41
           <img src="content/imgs/db-icon.png" style="width: 32px; height: 32px;"><br>Databases
29 42
         </li>
30 43
       </ul>
31 44
     </header>
32
-    <div class="well">
33
-      <p>Default View when opening the app:<br>
34
-      <select class="form-control" style="width: 220px;">
35
-        <option>All Notes</option>
36
-        <option>All Files</option>
37
-        <option>Index note</option>
38
-      </select></p>
39
-      <p>Default Notebook:
40
-      <select class="form-control" style="width: 220px;">
41
-        <option>Notebook 1</option>
42
-        <option>Notebook 2</option>
43
-        <option>Notebook 3</option>
44
-      </select></p>
45
+    <div ui-view class="well">
45 46
 
46 47
     </div>
47 48
   </body>

+ 31 - 0
app/scripts/controllers/databases-prefs-ctrl.js

@@ -0,0 +1,31 @@
1
+
2
+
3
+/**
4
+ * @ngdoc function
5
+ * @name domainManagerApp.controller:AboutCtrl
6
+ * @description
7
+ * # AboutCtrl
8
+ * Controller of the domainManagerApp
9
+ */
10
+
11
+angular.module('codexApp.prefs.databases', [])
12
+  .controller('DatabasesPrefsCtrl', ['$scope', '$rootScope', '$state', '$location', 'FileService', 'PrefsService', '$timeout', function ($scope,  $rootScope, $state, $location, FileService, PrefsService, $timeout) {
13
+
14
+    console.log("-> Preferences/General");
15
+    $scope.notesDir = FileService.getNotesDir();
16
+
17
+    $scope.changeDir = function(){
18
+      dialog.showOpenDialog({ defaultPath: FileService.getNotesDir(), properties: ['openDirectory'] }, function (dir) {
19
+        console.log("-> Changin folder location to: " + dir);
20
+        FileService.setNotesDir(dir);
21
+        if(!$scope.$$phase) {
22
+          $scope.$apply(function(){
23
+              $scope.notesDir = dir[0];
24
+          });
25
+        } else {
26
+            $scope.notesDir = dir[0];
27
+        }
28
+      });
29
+    }
30
+
31
+  }]);

+ 17 - 0
app/scripts/controllers/general-prefs-ctrl.js

@@ -0,0 +1,17 @@
1
+
2
+
3
+/**
4
+ * @ngdoc function
5
+ * @name domainManagerApp.controller:AboutCtrl
6
+ * @description
7
+ * # AboutCtrl
8
+ * Controller of the domainManagerApp
9
+ */
10
+
11
+angular.module('codexApp.prefs.general', [])
12
+  .controller('GeneralPrefsCtrl', ['$scope', '$rootScope', '$state', '$location', 'FileService', 'PrefsService', '$timeout', function ($scope,  $rootScope, $state, $location, FileService, PrefsService, $timeout) {
13
+
14
+    console.log("-> Preferences/General");
15
+
16
+
17
+  }]);

+ 33 - 0
app/scripts/controllers/prefs-nav-ctrl.js

@@ -0,0 +1,33 @@
1
+
2
+
3
+/**
4
+ * @ngdoc function
5
+ * @name domainManagerApp.controller:AboutCtrl
6
+ * @description
7
+ * # AboutCtrl
8
+ * Controller of the domainManagerApp
9
+ */
10
+
11
+angular.module('codexApp.prefs.nav', [])
12
+  .controller('PrefsNavCtrl', ['$scope', '$rootScope', '$state', '$location', 'FileService', 'PrefsService', '$timeout', function ($scope,  $rootScope, $state, $location, FileService, PrefsService, $timeout) {
13
+
14
+    $scope.links = [];
15
+    console.log("-> Preferences/General");
16
+    $scope.links[0] = "active";
17
+    $scope.links[1] = "";
18
+
19
+    $scope.goToGeneral = function () {
20
+      console.log("-> Preferences/General")
21
+      $scope.links[0] = "active";
22
+      $scope.links[1] = "";
23
+      $state.go("general");
24
+    }
25
+
26
+    $scope.goToDatabases = function () {
27
+      console.log("-> Preferences/Databases")
28
+      $scope.links[0] = "";
29
+      $scope.links[1] = "active";
30
+      $state.go("databases");
31
+    }
32
+
33
+  }]);

+ 36 - 0
app/scripts/preferences.js

@@ -0,0 +1,36 @@
1
+angular
2
+  .module('codexApp', [
3
+    'ui.router',
4
+    'ngSanitize',
5
+    'codexApp.prefs.nav',
6
+    'codexApp.prefs.general',
7
+    'codexApp.prefs.databases'
8
+
9
+  ])
10
+
11
+  .config(['$stateProvider', '$urlRouterProvider', '$httpProvider', function($stateProvider, $urlRouterProvider, $httpProvider) {
12
+
13
+    // Configs
14
+    //Enable cross domain calls
15
+    $httpProvider.defaults.useXDomain = true;
16
+    //Remove the header used to identify ajax call  that would prevent CORS from working
17
+    delete $httpProvider.defaults.headers.common['X-Requested-With'];
18
+
19
+    // UI router
20
+    // For any unmatched url, redirect to /state1
21
+
22
+    $stateProvider
23
+    .state('general', {
24
+      url: "/",
25
+      templateUrl: 'views/preferences/general.html',
26
+      controller: 'GeneralPrefsCtrl'
27
+    })
28
+    .state('databases', {
29
+      url: "/databases",
30
+      templateUrl: 'views/preferences/databases.html',
31
+      controller: 'DatabasesPrefsCtrl'
32
+    })
33
+    $urlRouterProvider.otherwise("/");
34
+
35
+
36
+  }]);

+ 38 - 6
app/scripts/services/file-service.js

@@ -1,15 +1,21 @@
1 1
 angular.module('codexApp')
2 2
 .service('FileService', [ '$rootScope', '$http', 'ThumbnailService', '$state',  function($rootScope, $http, ThumbnailService, $state) {
3 3
 
4
+  var defaultUserContentPath = "";
5
+  var appDataPath = "";
6
+  var appData = {};
7
+  var notes_dir = "";
8
+
4 9
   var getAppData = function(){
5 10
     var remote = require('remote');
6 11
     var app = remote.require('app');
7
-    var appDataPath = app.getPath("userData");
8
-    var defaultUserContentPath = app.getPath("home") + "/Documents";
12
+    appDataPath = app.getPath("userData");
13
+    defaultUserContentPath = app.getPath("home") + "/Documents/codex";
9 14
     findOrGenerateUserDataFile(appDataPath, defaultUserContentPath);
10 15
     var raw_data = fs.readFileSync(appDataPath + '/userData.json', 'utf8');
11 16
     var data = JSON.parse(raw_data);
12
-    console.log(data);
17
+    appData = data;
18
+    notes_dir = appData.UserDataDirectory;
13 19
     return data
14 20
   }
15 21
 
@@ -27,14 +33,31 @@ angular.module('codexApp')
27 33
       file_path = path + "/UserData.json";
28 34
       console.log("-> Generating user settings file: '" + file_path + "'");
29 35
       var content = '{ "UserDataDirectory" : "' + defaultUserContentPath +'" }';
30
-      fs.writeFileSync(file_path, content, 'utf8');
36
+      mkdirSync(defaultUserContentPath);
37
+      console.log(content);
38
+      saveAppData(JSON.parse(content));
31 39
       return true;
32 40
     }
33 41
   }
34 42
 
35
-  var appData = getAppData();
43
+  var saveAppData = function(data) {
44
+    console.log("-> Saving user data...");
45
+    console.log(data);
46
+    fs.writeFileSync(appDataPath + "/UserData.json", JSON.stringify(data), 'utf8');
47
+  }
48
+
49
+  var mkdirSync = function (path) {
50
+    try {
51
+      fs.mkdirSync(path);
52
+    } catch(e) {
53
+      if ( e.code != 'EEXIST' ) throw e;
54
+    }
55
+  }
56
+
57
+  getAppData();
58
+  console.log("-> Loading content from folder: " + appData.UserDataDirectory);
59
+
36 60
 
37
-  var notes_dir = appData.UserDataDirectory;
38 61
   var default_notes_dir = "/Users/james/dev/codex/codex/inbox";
39 62
   var default_home_note = "/Users/james/dev/codex/codex/index.md"
40 63
   var notes = [];
@@ -436,18 +459,21 @@ angular.module('codexApp')
436 459
 
437 460
   // RESPONSE
438 461
   this.getAllFiles = function(dir) {
462
+    getAppData();
439 463
     if (typeof(dir)==='undefined') dir = notes_dir;
440 464
     notes = getAllFilesFromFolder(dir);
441 465
     return notes.sort(date_sort_asc);
442 466
   }
443 467
 
444 468
   this.getFiles = function(dir) {
469
+    getAppData();
445 470
     if (typeof(dir)==='undefined') dir = notes_dir;
446 471
     notes = getFilesFromFolder(dir);
447 472
     return notes.sort(date_sort_asc);
448 473
   }
449 474
 
450 475
   this.getAllNotes = function() {
476
+    getAppData();
451 477
     notes = getAllFilesFromFolder();
452 478
     notes = filterNotes(notes);
453 479
     return notes.sort(date_sort_asc);
@@ -493,6 +519,12 @@ angular.module('codexApp')
493 519
     }
494 520
   }
495 521
 
522
+  this.setNotesDir = function(dir) {
523
+    appData.UserDataDirectory = dir[0];
524
+    saveAppData(appData);
525
+    notes_dir = dir[0];
526
+  }
527
+
496 528
   this.getNotesDir = function() {
497 529
     return notes_dir;
498 530
   }

+ 4 - 0
app/views/preferences/databases.html

@@ -0,0 +1,4 @@
1
+<p>Root Folder:<br>
2
+<textarea class="form-control well" rows="2" disabled style="background-color: #E2E2E2; border-color: #DEDEDE; float: left; width: 380px; margin-right: 25px;">{{notesDir}}</textarea>
3
+<button class="btn btn-mini btn-default" ng-click="changeDir()">Change</button>
4
+</p>

+ 14 - 0
app/views/preferences/general.html

@@ -0,0 +1,14 @@
1
+<p>Default View when opening the app:<br>
2
+<select class="form-control" style="width: 220px;">
3
+  <option>All Notes</option>
4
+  <option>All Files</option>
5
+  <option>Index note</option>
6
+</select></p>
7
+<p>
8
+  Default Notebook:
9
+  <select class="form-control" style="width: 220px;">
10
+    <option>Notebook 1</option>
11
+    <option>Notebook 2</option>
12
+    <option>Notebook 3</option>
13
+  </select>
14
+</p>